# -*- coding: utf-8 -*-
import scrapy
from ..items import MaoyanItem


class MaoyanSpider(scrapy.Spider):
    name = 'maoyan'
    allowed_domains = ['maoyan.com']
    start_urls = ['https://maoyan.com/board/4?offset=0']
    offset = 0

    def parse(self, response):
        item_info_list = response.xpath('//dl[@class="board-wrapper"]/dd//div[@class="movie-item-info"]')
        for item_info in item_info_list:
            # 创建item对象
            item = MaoyanItem()
            # extract_first() 取出第一个文本(获取列表中第一个序列化的元素(字符串))  extract()将所有的元素序列化为Unicode字符串
            item['name'] = item_info.xpath('./p[@class="name"]/a/text()').extract_first().strip()
            item['star'] = item_info.xpath('./p[@class="star"]/text()').extract_first().strip()
            item['time'] = item_info.xpath('./p[@class="releasetime"]/text()').extract_first().strip()[5:]

            # 将item提交给管道pipeline 在setting中设置ITEM_PIPELINES 优先级1-1000,越小优先级越高
            yield item

        self.offset += 10
        if self.offset < 100:
            next_url = 'https://maoyan.com/board/4?offset={}'.format(self.offset)
            # 交给调度器入队列
            yield scrapy.Request(
                url=next_url,
                callback=self.parse
            )





